package com.youshang520i.springcommonmiddleware.mq.rabbitmq.CommonUtils;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.youshang520i.springcommonmiddleware.mq.rabbitmq.model.RabbitMqConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Component
public class RabbitMQConnectionUtils {

    @Autowired
    private static RabbitMqConnection rabbitMqConnection;


    public static Connection getConnection() throws IOException, TimeoutException {
        //定义连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPassword(rabbitMqConnection.getPassword());
        factory.setUsername(rabbitMqConnection.getUsername());
        factory.setPort(rabbitMqConnection.getPort());
        factory.setHost(rabbitMqConnection.getHost());
        return factory.newConnection();
    }
}
